package cn.dida.service.impl;

import cn.dida.dao.LgsNodeDao;
import cn.dida.dao.LgsPdaUserDao;
import cn.dida.model.LgsNode;
import cn.dida.model.LgsPdaUser;
import cn.dida.model.Pageable;
import cn.dida.service.LgsPdaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by zhangzhiling on 2017/2/8.
 */
@Service
public class LgsPdaUserServiceImpl implements LgsPdaUserService{

    @Autowired
    private LgsPdaUserDao lgsPdaUserDao;

    @Autowired
    private LgsNodeDao lgsNodeDao;

    /**
     * pda获取用户列表
     * @param companyId
     * @param keyword
     * @param realName
     * @param depotName
     * @param sta
     * @param page
     * @return
     */
    @Override
    public Pageable<LgsPdaUser> getList(Integer companyId, String keyword,String realName,String depotName, Integer sta, Integer page) {
        return lgsPdaUserDao.query(companyId, keyword,realName,depotName, sta, page);
    }

    /**
     * 获取操作类型列表
     * @param companyId
     * @param sta
     * @return
     */
    @Override
    public List<LgsNode> getNodeList(Integer companyId, Integer sta){
        return lgsNodeDao.getNodeList(companyId,sta);
    }

    /**
     * 添加pda用户
     * @param lgsPdaUser
     * @return
     */
    @Override
    @Transactional
    public Boolean insert(LgsPdaUser lgsPdaUser) {
        String[] nodes = lgsPdaUser.getNodes().split(",");
        Integer userId = lgsPdaUserDao.insert(lgsPdaUser);
        for(int i=0; i<nodes.length; i++){
            if(nodes[i] != "") {
                lgsPdaUserDao.insertPuserNode(userId, Integer.parseInt(nodes[i]));
            }
        }

        return true;
    }

    /**
     * 修改pda用户
     * @param lgsPdaUser
     * @return
     */
    @Override
    public Boolean update(LgsPdaUser lgsPdaUser){

        String[] nodes = lgsPdaUser.getNodes().split(",");
        Integer userId = lgsPdaUser.getId();
        lgsPdaUserDao.deletPuserNode(userId);
        for(int i=0; i<nodes.length; i++){
            if(nodes[i] != ""){
                lgsPdaUserDao.insertPuserNode(userId, Integer.parseInt(nodes[i]));
            }
        }
        lgsPdaUserDao.update(lgsPdaUser);
        return true;
    }

    /**
     * 获取pda用户详情
     * @param id
     * @return
     */
    @Override
    public LgsPdaUser getPdaUser(Integer id) {
        return lgsPdaUserDao.getPdaUser(id);
    }

    /**
     * 获取用户关联操作类型
     * @param userId
     * @return
     */
    public List<Map<String,Object>> getNodeByUser(Integer userId){
        return lgsPdaUserDao.getNodeByUser(userId);
    }

    /**
     * 验证
     * 用户名是否已存在
     * @param account
     * @return  FALSE：不存在； TRUE：存在
     */
    @Override
    public Boolean testAccount(String account){

        LgsPdaUser lgsPdaUser = lgsPdaUserDao.testAccount(account);
        if(lgsPdaUser == null){
            return false;

        }else{
            return true;
        }
    }

    /**
     * pda用户数据导出
     * @param companyId
     * @param keyword
     * @param realName
     * @param depotName
     * @param sta
     * @return
     */
    @Override
    public List<LgsPdaUser> queryForExport(Integer companyId, String keyword, String realName, String depotName, Integer sta){
        return lgsPdaUserDao.queryForExport(companyId, keyword, realName, depotName, sta);
    }
}
